<?php

class SettingController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
		array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','addSetting','deleteSetting','getSetting'),
				'users'=>array('*'),
		),
		array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
		),
		array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
		),
		array('deny',  // deny all users
				'users'=>array('*'),
		),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 0 means admin, 1 means super member and 2 means member
	 */
	public function actionAddSetting() {
		$response = "<Response>";
		if ( !isset($_POST['request'] )){
			$response = $response."<errorcode>000</errorcode><message>Request paramater is not found.</message></Response>";
			echo $response;
			return;
		}
		$request = $_POST['request'];
		
		$response = "<Response>";
		$xml = simplexml_load_string($request);
		$username = $xml->username;
		$password = $xml->password;
		$country = $xml->country;
		$errorMsg = $this->authenticate($username,$password);
		if ($errorMsg != "") {
			$response = $response."<errorcode>000</errorcode><message>".$errorMsg."</message></Response>";
			echo $response;
			return;
		}

		$cModel = Country::model()->findByAttributes(array('code' => $xml->country));
		if (!isset($cModel)){
			$response = $response."<errorcode>000</errorcode><message>Country is not valid.</message></Response>";
			echo $response;
			return;
		}
		$record = UserAccount::model()->findByAttributes(array('username' => $username));

		$model = new Setting;
		$id = $xml->id;
		if($id != ""){
			$model=Setting::model()->findByPk($id);
		}
		$model->user_id = $record->id;
		$model->post_per_ip = $xml->post_per_ip;;
		$model->post_interval = $xml->post_interval;
		$model->upper_age = $xml->upper_age;
		$model->lower_age = $xml->lower_age;
		$model->login_cl = $xml->login_cl;
		$model->use_location = $xml->use_location;
		$model->random_subarea = $xml->random_subarea;
		$model->use_age = $xml->use_age;
		$model->use_fw_email = $xml->use_fw_email;
		$model->max_wait_time = $xml->max_wait_time;
		$model->log_auto_scroll = $xml->log_auto_scroll;
		$model->post_type = $xml->post_type;
		$model->auto_post = $xml->auto_post;
		$model->change_ip = $xml->change_ip;
		$model->user_name = $xml->user_name;
		$model->password = $xml->upass;
		$model->country_id = $cModel->id;
		$model->use_proxy = $xml->use_proxy;
		$model->postfix = $xml->postfix;
		$model->prefix = $xml->prefix;
		$model->vpn_name = $xml->vpn_name;
		$model->max_try = $xml->max_try;
                $model->ie_path = $xml->ie_path;
                $model->ff_path = $xml->ff_path;
                $model->gc_path = $xml->gc_path;
                $model->driver = $xml->driver;
                $model->accountid = $xml->accountid;
                $model->vpn = $xml->vpn;
                $model->web_email = $xml->web_email;
                $model->close_browser = $xml->close_browser;
                $model->random = $xml->random;
                $model->delete_post = $xml->delete_post;
                $model->update_account = $xml->update_account;
                $model->proxy_type = $xml->proxy_type;
                $model->use_slot = $xml->use_slot;
                $model->post_insame_session = $xml->post_insame_session;
                $model->renew = $xml->renew;
                $model->hide_resources = $xml->hide_resources;
                $model->slot_no = $xml->slot_no;
                $model->job = $xml->job;
                $model->run_individual = $xml->run_individual;
                $model->domain_info = $xml->domain_info;
                $model->email_read_try = $xml->email_read_try;
		if($id != ""){
			if($model->update()){
				$response = $response."<errorcode>001</errorcode><message>Setting has been updated successfully.</message></Response>";
			} else {
				$response = $response."<errorcode>000</errorcode><message>Problem in setting update. Please contact with system administrator</message></Response>";
			}
		} else {
			if($model->save()){
				$response = $response."<errorcode>001</errorcode><message>Setting has been created successfully.</message></Response>";
			} else {
				$response = $response."<errorcode>000</errorcode><message>Problem in setting creation. Please contact with system administrator</message></Response>";
			}
		}
		echo $response;
		//print_r($xml);
	}

	public function deleteSetting($request) {
		$response = "<Response>";
		$xml = simplexml_load_string($request);
		$username = $xml->username;
		$password = $xml->password;
		$id = $xml->id;
		$errorMsg = $this->authenticate($username,$password);
		if ($errorMsg != "") {
			$response = $response."<errorcode>000</errorcode><message>".$errorMsg."</message></Response>";
			echo $response;
			return;
		}

		$model = Setting::model()->findByPK($id);
		if($model->delete()){
			$response = $response."<errorcode>001</errorcode><message>Setting has been deleted successfully.</message></Response>";
		} else {
			$response = $response."<errorcode>000</errorcode><message>Problem in setting delete. Please contact with system administrator</message></Response>";
		}
		echo $response;
	}

	public function actionGetSetting($username,$password) {
		$response = "<Response>";
		$errorMsg = $this->authenticate($username,$password);
		if ($errorMsg != "") {
			$response = $response."<errorcode>000</errorcode><message>".$errorMsg."</message></Response>";
			echo $response;
			return;
		}

		$record = UserAccount::model()->findByAttributes(array('username' => $username));
		$list = Setting::model()->findAll("user_id=:user", array("user" => $record->id));

		if (!isset($list) || empty($list)){
			echo "";
			return ;
		}
		$setting = $list[0];
		$childRes = "<setting>";
		$childRes = $childRes."<id>".$setting->id."</id>";
		$childRes = $childRes."<user_id>".$setting->user_id."</user_id>";
		$childRes = $childRes."<post_per_ip>".$setting->post_per_ip."</post_per_ip>";
		$childRes = $childRes."<post_interval>".$setting->post_interval."</post_interval>";
		$childRes = $childRes."<upper_age>".$setting->upper_age."</upper_age>";
		$childRes = $childRes."<lower_age>".$setting->lower_age."</lower_age>";
		$childRes = $childRes."<login_cl>".$setting->login_cl."</login_cl>";
		$childRes = $childRes."<use_location>".$setting->use_location."</use_location>";
		$childRes = $childRes."<random_subarea>".$setting->random_subarea."</random_subarea>";
		$childRes = $childRes."<use_age>".$setting->use_age."</use_age>";
		$childRes = $childRes."<use_fw_email>".$setting->use_fw_email."</use_fw_email>";
		$childRes = $childRes."<max_wait_time>".$setting->max_wait_time."</max_wait_time>";
		$childRes = $childRes."<log_auto_scroll>".$setting->log_auto_scroll."</log_auto_scroll>";
		$childRes = $childRes."<post_type>".$setting->post_type."</post_type>";
		$childRes = $childRes."<auto_post>".$setting->auto_post."</auto_post>";
		$childRes = $childRes."<change_ip>".$setting->change_ip."</change_ip>";
		$childRes = $childRes."<user_name>".$setting->user_name."</user_name>";
		$childRes = $childRes."<upass>".$setting->password."</upass>";
		$childRes = $childRes."<user_name>".$setting->user_name."</user_name>";
		$childRes = $childRes."<country_id>".$setting->country_id."</country_id>";
		$childRes = $childRes."<use_proxy>".$setting->use_proxy."</use_proxy>";
		$childRes = $childRes."<postfix>".$setting->postfix."</postfix>";
		$childRes = $childRes."<prefix>".$setting->prefix."</prefix>";
		$childRes = $childRes."<vpn_name>".$setting->vpn_name."</vpn_name>";
		$childRes = $childRes."<max_try>".$setting->max_try."</max_try>";
		$childRes = $childRes."<ie_path>".$setting->ie_path."</ie_path>";
                $childRes = $childRes."<ff_path>".$setting->ff_path."</ff_path>";
                $childRes = $childRes."<gc_path>".$setting->gc_path."</gc_path>";
                $childRes = $childRes."<driver>".$setting->driver."</driver>";
                $childRes = $childRes."<accountid>".$setting->accountid."</accountid>";
                $childRes = $childRes."<vpn>".$setting->vpn."</vpn>";
                $childRes = $childRes."<web_email>".$setting->web_email."</web_email>";
                $childRes = $childRes."<close_browser>".$setting->close_browser."</close_browser>";
                $childRes = $childRes."<random>".$setting->random."</random>";
                $childRes = $childRes."<delete_post>".$setting->delete_post."</delete_post>";
                $childRes = $childRes."<update_account>".$setting->update_account."</update_account>";
                $childRes = $childRes."<proxy_type>".$setting->proxy_type."</proxy_type>";
                $childRes = $childRes."<use_slot>".$setting->use_slot."</use_slot>";
                $childRes = $childRes."<post_insame_session>".$setting->post_insame_session."</post_insame_session>";
                $childRes = $childRes."<renew>".$setting->renew."</renew>";
                $childRes = $childRes."<hide_resources>".$setting->hide_resources."</hide_resources>";
                $childRes = $childRes."<slot_no>".$setting->slot_no."</slot_no>";   
                $childRes = $childRes."<job>".$setting->job."</job>";   
                $childRes = $childRes."<run_individual>".$setting->run_individual."</run_individual>";   
                $childRes = $childRes."<domain_info>".$setting->domain_info."</domain_info>";   
                $childRes = $childRes."<email_read_try>".$setting->email_read_try."</email_read_try>";   
                $childRes = $childRes."</setting>";

		$response = $response."<errorcode>001</errorcode><message>Record found</message>";
		$response = $response.$childRes."</Response>";
		echo $response;
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Setting;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Setting']))
		{
			$model->attributes=$_POST['Setting'];
			if($model->save())
			$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Setting']))
		{
			$model->attributes=$_POST['Setting'];
			if($model->save())
			$this->redirect(array('view','id'=>$model->id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
		$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Setting');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Setting('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Setting']))
		$model->attributes=$_GET['Setting'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Setting the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Setting::model()->findByPk($id);
		if($model===null)
		throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Setting $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='setting-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
